﻿// =================================================================== 
// 数据访问层(GDIT.DAL)
//====================================================================
// 文件：DRoles.cs
// 项目名称：学院网站
// 创建时间：2012-08-08
// 负责人：ChenZhiGang
//引用DevExpress.Data.Filtering，DevExpress.Xpo程序集
// 程序集介绍：角色信息数据访问层
// ===================================================================
using GDIT.Model;
using DevExpress.Data.Filtering;
using DevExpress.Xpo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GDIT.Pattern;

namespace GDIT.DAL
{
    public class DRoles : IRoles
    {

        private XpoHelper.Test<Mtb_Roles> userOperation = null;

        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="detailMenu">需要新增的角色对象</param>
        /// <returns>1：新增成功 2：新增失败</returns>
        public int InsertRoles(Mtb_Roles roles)
        {
            return XpoHelper.SaveAdded(roles);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="detailMenu">需要删除的角色对象</param>
        /// <returns>1：删除成功 0：删除失败</returns>
        public int DeleteRoles(Mtb_Roles roles)
        {
            userOperation = new XpoHelper.Test<Mtb_Roles>();
            return userOperation.Delete(roles.OID);
        }

        /// <summary>
        /// 更新角色
        /// </summary>
        /// <param name="detailMenu">需要更新的角色对象</param>
        /// <returns>1：更新成功 0：更新失败</returns>
        public int UpdateRoles(Mtb_Roles roles)
        {
            userOperation = new XpoHelper.Test<Mtb_Roles>();
            XPCollection xp = userOperation.Operation("OID", roles.OID);
            foreach (Mtb_Roles b in xp)
            {
                b.RoleName = roles.RoleName;
                return XpoHelper.SaveModified(b);
            }
            return 0;
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="roles"></param>
        /// <returns></returns>
        public int InsertUserToRoles(Mtb_Roles roles)
        {
            userOperation = new XpoHelper.Test<Mtb_Roles>();
            XPCollection xp = userOperation.Operation("OID", roles.OID);
            foreach (Mtb_Roles b in xp)
            {
                b.Users.AddRange(roles.Users);
                return XpoHelper.SaveModifiedByZi(b);
            }
            return 0;
        }

        /// <summary>
        /// 查询所有的角色列表
        /// </summary>
        /// <returns>所有的角色列表</returns>
        public XPCollection SelectRoles()
        {
            userOperation = new XpoHelper.Test<Mtb_Roles>();
            return userOperation.Operation();
        }

        /// <summary>
        /// 查询单一的角色对象
        /// </summary>
        /// <returns>单一的角色对象</returns>
        public Mtb_Roles SelectRolesByRoles(Mtb_Roles roles)
        {
            Mtb_Roles bulletin = null;
            userOperation = new XpoHelper.Test<Mtb_Roles>();
            XPCollection xp = userOperation.Operation("OID", roles.OID);
            foreach (Mtb_Roles b in xp)
            {
                bulletin = b;
            }
            return bulletin;
        }
    }
}
